package com.dzm.test08.manager;

import com.dzm.test08.entity.Student;

/**
 * 作者：Dzm
 * 时间：2024-07-13-10:30
 * 功能：
 */
public class StudentManager {
    //学生列表
    private Student[] students = new Student[10];
    //学生下标
    private int count;

    public StudentManager() {
        initial();
    }

    //初始化学生信息
    private void initial() {
        Student s1 = new Student(1001, "张三", "男", 20, "上海", 1);
        Student s2 = new Student(1002, "李四", "男", 22, "武汉", 2);
        Student s3 = new Student(1003, "王五", "女", 21, "广州", 3);
        students[count++] = s1;
        students[count++] = s2;
        students[count++] = s3;
    }

    public void searchStudents(Student... myStudents) {
        if(myStudents.length == 0) {
            myStudents = students;
        }

        System.out.println("学生信息如下：");
        System.out.println("\t学号\t姓名\t性别\t年龄\t住址\t班级编号");
        System.out.println("-----------------------------------------------");
        for (Student student : myStudents) {
            if(student != null) {
                System.out.println(student);
            }
        }
        System.out.println("-----------------------------------------------");

    }

    public void addStudent(Student student) {
        //遍历学生列表是否出现重复id
        for (Student s : students) {
            if(s != null && s.getSid() == student.getSid()) {
                System.out.println("学号已存在！");
                return;
            }
        }

        //判断列表是否满
        if(count < students.length) {
            students[count++] = student;
            System.out.println("添加成功！");
        } else {
            System.out.println("学生列表已满！");
        }
    }

    public void findStudentsByKeyword(String keyword) {
        //定义一个新数组用于存放查找到的学生
        Student[] search = new Student[students.length];
        //计数器
        int n = 0;

        for (Student student : students) {
            if(student != null) {
                //根据姓名，性别，住址查找
                if(student.getSname().contains(keyword) || student.getSex().contains(keyword) || student.getAddr().contains(keyword)) {
                    search[n++] = student;
                }
            }
        }

        if(n > 0) {
            searchStudents(search);
        } else {
            System.out.println("未找到该学生！");
        }
    }

    //通过学号查找学生
    public Student findStudentById(int id) {
        for (Student student : students) {
            if(student != null && student.getSid() == id) {
                return student;
            }
        }
        return null;
    }
}
